<?php

// check user login
session_start();
if (isset($_SESSION["user"]) && !empty($_SESSION["user"])) {
//echo $_SESSION["user"];




class Messages extends CI_Controller {

	public function __construct() {
            parent::__construct(); 

            $this->load->helper(array('form','url'));
            $this->load->library('form_validation');
	}
        
        public function index() {
            
            
        }   ////// end of function index
        ////////////////////////////////////////////////
        
        public function call_mail_creation_view() {
            
            $this->load->view('toolbar_view');
            $this->load->model('common_models');
            $familyids['data'] = $this->common_models->get_active_family_information();
            $this->load->view('messages/mail_create_view', $familyids);
            
            $this->call_mail_reading_view();
        }
        
        public function call_mail_reading_view() {
            
            $this->load->view('toolbar_view');
            
            $this->load->model('messaging_models');
            $mail_data['maildata'] = $this->messaging_models->get_my_unread_messages();
            $this->load->view('messages/mail_reading_view', $mail_data);
		//print_r($mail_data);
            
            
            
        }
        
        
        
        
        public function mail_create_submit() {
            
            if ($this->_msg_validate() === FALSE) {
                $this->call_mail_creation_view();
                return;    
            }
            $this->load->model('messaging_models');
            $this->messaging_models->send_message();
            
            $this->call_mail_creation_view();
            //$this->call_mail_reading_view();
            
        }////// end of function mail_create_submit
        ////////////////////////////////////////////////
        
        
        
        private function _msg_validate() {
            
            $this->form_validation->set_rules('msgbody', 'msgbody',
                    'required|min_length[15]|trim|max_length[255]|xss_clean');
            $this->form_validation->set_rules('importance', 'importance',
                    'required|min_length[1]|trim|max_length[2]|xss_clean');
            $this->form_validation->set_rules('msgtype', 'msgtype',
                    'required|min_length[1]|trim|max_length[2]|xss_clean');
            $this->form_validation->set_rules('family_from', 'family_from',
                    'required|min_length[1]|trim|max_length[6]|xss_clean');
            $this->form_validation->set_rules('family_to', 'family_to',
                    'required|min_length[1]|trim|max_length[6]|xss_clean');
            
            return $this->form_validation->run();
            
        }
        
        
         
                 
         
         
         
    }    /////////////// END: class Messages extends CI_Controller 
    
}  //// END:  if (isset($_SESSION["user"]) && !empty($_SESSION["user"]))
else {
   // deny, log, redirect
    
    //$this->load->helper('url');
    //redirect('/dynasties2/start', 'location');
    
}


